package algorithm;

import java.util.HashMap;
import java.util.Map;

public class T00003 {
    public static void main(String[] args) {
        String str = "pwwkew";
        System.out.println(new Solution().lengthOfLongestSubstring(str));
    }

    static class Solution {
        public int lengthOfLongestSubstring(String s) {
            Map<Character, Integer> window = new HashMap<>(s.length());
            int left = 0, right = 0, start = 0, len = 0;
            while (right < s.length()) {
                char c = s.charAt(right);
                window.put(c, window.getOrDefault(c, 0) + 1);
                right ++;

                if (window.get(c) > 1) {
                    while (window.get(c) > 1) {
                        char d = s.charAt(left);
                        window.put(d, window.get(d) - 1);
                        left ++;
                    }
                } else {
                    if (right - left > len) {
                        start = left;
                        len = right - left;
                    }
                }
            }
            return len;
        }
    }
}
